﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeveloperControl.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the DeveloperControl class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the DeveloperControl class.
    /// </summary>
    public static class DeveloperControl
    {
        /// <summary>
        /// Selects a DeveloperControl with the given identifier.
        /// </summary>
        /// <param name="developerControlId">The identifier of the desired DeveloperControl.</param>
        /// <returns>An instantiated DeveloperControl.</returns>
        public static BusinessObjects.DeveloperControl Select(int developerControlId)
        {
            BusinessObjects.DeveloperControl developerControl = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetDeveloperControlByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DeveloperControlID", developerControlId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            developerControl = new BusinessObjects.DeveloperControl();
                            rdr.Read();
                            developerControl.Id = developerControlId;
                            developerControl.Name = (string)rdr["DeveloperControlName"];
                            developerControl.Path = (string)rdr["DeveloperControlPath"];
                            developerControl.Description = (string)rdr["DeveloperControlDescription"];
                        }
                    }
                }
            }

            return developerControl;
        }
        
        /// <summary>
        /// Gets a dictionary of DeveloperControls for the current application.
        /// </summary>
        /// <param name="area">The area of DeveloperControls to return (Main or Right).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced DeveloperControl's name.</returns>
        public static List<BusinessObjects.DeveloperControl> SelectAll()
        {
            BusinessObjects.DeveloperControl developerControl = null;
            List<BusinessObjects.DeveloperControl> developerControls = new List<BusinessObjects.DeveloperControl>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetDeveloperControls", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                developerControl = new BusinessObjects.DeveloperControl();
                                developerControl.Id = Convert.ToInt32(rdr["DeveloperControlID"], CultureInfo.InvariantCulture);
                                developerControl.Name = (string)rdr["DeveloperControlName"];
                                developerControl.Path = (string)rdr["DeveloperControlPath"];
                                developerControl.Description = (string)rdr["DeveloperControlDescription"];
                                developerControls.Add(developerControl);
                            }
                        }
                    }
                }
            }

            return developerControls;
        }

        /// <summary>
        /// Removes all references to this DeveloperControl, as well as the DeveloperControl registration. It does not delete the ascx file.
        /// </summary>
        /// <param name="developerControl">The DeveloperControl to delete.</param>
        public static void Delete(BusinessObjects.DeveloperControl developerControl)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteDeveloperControl", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DeveloperControlID", developerControl.Id);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Inserts the DeveloperControl object into the database.
        /// </summary>
        /// <param name="developerControl">The DeveloperControl to insert.</param>
        /// <returns>The inserted DeveloperControl with a newly populated Id field.</returns>
        public static BusinessObjects.DeveloperControl Insert(BusinessObjects.DeveloperControl developerControl)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertDeveloperControl", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DeveloperControlName", developerControl.Name);
                    cmd.Parameters.AddWithValue("@DeveloperControlPath", developerControl.Path);
                    cmd.Parameters.AddWithValue("@DeveloperControlDescription", developerControl.Description);
                    developerControl.Id = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return developerControl;
        }
    }
}